package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.LoanNewAdvertCreditCardRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.LoanNewAdvertCreditCardRequest;
import com.tuanzi.loan.vo.response.family.LoanNewAdvertCreditCardResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanNewAdvertCreditCard.LOAN_NEW_ADVERT_CREDIT_CARD;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanNewCreditCardInfo.LOAN_NEW_CREDIT_CARD_INFO;

/**
 * User: liangZongXi
 * Create time: 11:14 2018/9/5
 * Description:
 */

@Repository
public class LoanNewAdvertCreditCardRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanNewAdvertCreditCardRepositoryCustom{

    @Override
    public Page<LoanNewAdvertCreditCardResponse> list(LoanNewAdvertCreditCardRequest searchRequest, Pageable pageable) {

        SelectConditionStep<Record> sql = dslContext.select(LOAN_NEW_ADVERT_CREDIT_CARD.fields())
                .select(LOAN_NEW_CREDIT_CARD_INFO.ID.as("card_info_id"),LOAN_NEW_CREDIT_CARD_INFO.LOGO,LOAN_NEW_CREDIT_CARD_INFO.CARD_NAME)
                .from(LOAN_NEW_ADVERT_CREDIT_CARD)
                .leftJoin(LOAN_NEW_CREDIT_CARD_INFO)
                .on(LOAN_NEW_ADVERT_CREDIT_CARD.CARD_ID.eq(LOAN_NEW_CREDIT_CARD_INFO.ID))
                .where(DSL.trueCondition());
        String platform = searchRequest.getPlatform();

        if(StringUtils.isNotBlank(platform)){
            sql.and(LOAN_NEW_ADVERT_CREDIT_CARD.PLATFORM.eq("0"));
            if(!"0".equals(platform)){
                sql.or(LOAN_NEW_ADVERT_CREDIT_CARD.PLATFORM.eq(platform));
            }
        }

        String prdid = searchRequest.getPrdid();
        if(StringUtils.isNotBlank(prdid)){
            sql.and(LOAN_NEW_ADVERT_CREDIT_CARD.PRDID.contains("9999"));
            if(!prdid.equals("9999")){
                sql.or(LOAN_NEW_ADVERT_CREDIT_CARD.PRDID.contains(prdid));
            }
        }

        String tag = searchRequest.getTag();
        if(StringUtils.isNotBlank(tag)){
            sql.and(LOAN_NEW_CREDIT_CARD_INFO.CARD_LABEL.contains(tag));
        }

        Integer status = searchRequest.getStatus();
        if(null != status){
            sql.and(LOAN_NEW_ADVERT_CREDIT_CARD.STATUS.eq(status));
        }

        Integer advertSpace = searchRequest.getAdvertSpace();
        if(null != advertSpace){
            sql.and(LOAN_NEW_ADVERT_CREDIT_CARD.ADVERT_ID.eq(0));
            if(advertSpace != 0){
                sql.or(LOAN_NEW_ADVERT_CREDIT_CARD.ADVERT_ID.eq(advertSpace));
            }
        }

        sql.orderBy(LOAN_NEW_ADVERT_CREDIT_CARD.SORT.asc());

        return search(sql.getQuery(),pageable, new ResponseMapper());
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_NEW_ADVERT_CREDIT_CARD;
    }


    private final class ResponseMapper implements RecordMapper<Record,LoanNewAdvertCreditCardResponse>{
        @Override
        public LoanNewAdvertCreditCardResponse map(Record record) {
            LoanNewAdvertCreditCardResponse response = new LoanNewAdvertCreditCardResponse();
            response.setId(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.ID));
            response.setCardId((Long)record.get("card_info_id"));
            response.setCardName(record.get(LOAN_NEW_CREDIT_CARD_INFO.CARD_NAME));
            response.setPlatform(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.PLATFORM));
            response.setPrdid(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.PRDID));
            response.setAdvertId(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.ADVERT_ID));
            response.setChannel(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.CHANNEL));
            response.setLogo(record.get(LOAN_NEW_CREDIT_CARD_INFO.LOGO));
            response.setOnlineTime(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.ONLINE_TIME));
            response.setOnlineTime(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.OFFLINE_TIME));
            response.setStatus(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.STATUS));
            response.setSort(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.SORT));
            response.setUpdator(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.UPDATOR));
            response.setUpdateTime(record.get(LOAN_NEW_ADVERT_CREDIT_CARD.UPDATE_TIME));

            return response;
        }
    }
}
